﻿using KTD.Web.BLL;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XXS.Common;
using XXS.Common.ControllerExt;

namespace KTD.Web.Controllers
{
    public class AdvController : Controller
    {
       
        // GET: Adv
        public ActionResult Index(AdvQD im)
        {
            im.PageSize = 10;
            ViewBag.Type = im.Type;
            var res = MF.AdvManager.GetListBy(im);
            return View(res);
        }

        [HttpPost]
        public ActionResult Create(AdvCUD im)
        {
            im.thumb = $"/Attachment/{Guid.NewGuid().ToString()}/{im.file.FileName}";
            var combinePath = Server.MapPath($"~{im.thumb }");

            Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

            im.file.SaveAs(combinePath);

            MF.AdvManager.Create(im);
            return this.JsonNet(new { succuess = true }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult Create(string Type)
        {
            ViewBag.appid = MF.HouseManager.GetSelect();
            ViewBag.CityList = MF.AreaManager.GetSelect();
            var model = new AdvCUD();
            model.type = Type;
            return View(model);
        }



        [HttpGet]
        public ActionResult Update(int Id)
        {
            ViewBag.appid = MF.HouseManager.GetSelect();
            ViewBag.CityList = MF.AreaManager.GetSelect();
            var model = MF.AdvManager.Find(Id);
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Update(AdvCUD im)
        {
            if (im.file != null&&im.file.ContentLength>0)
            {
                im.thumb = $"/Attachment/{Guid.NewGuid().ToString()}/{im.file.FileName}";
                var combinePath = Server.MapPath($"~{im.thumb }");

                Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

                im.file.SaveAs(combinePath);
            }


            MF.AdvManager.Update(im);
            return this.JsonNet(new { succuess = true }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete(int Id)
        {
            try
            {
                MF.AdvManager.Delete(Id);
                return this.JsonNet(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult DeleteImg(int Id)
        {
            try
            {
                MF.AdvManager.DeleteImg(Id);
                return this.JsonNet(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Find(int Id)
        {
            try
            {
                var res = MF.AdvManager.Find(Id);
                return this.JsonNet(new { success = true, data = res }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
        [OutputCache(Duration = 60 * 60, VaryByParam = "*")]
        public ActionResult GetList(AdvQD im)
        {
            try
            {
                var res = MF.AdvManager.GetListBy(im);
                return this.JsonNet(res.PageData.ToExtGrid(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }


        public ActionResult Notice()
        {
            var res = MF.AdvManager.GetListBy(new AdvQD { Type="公告" });
            return View(res);
        }
        [HttpGet]
        public ActionResult UpdateNotice(int Id)
        {
            var model = MF.AdvManager.Find(Id);
            return View(model);
        }

        public ActionResult FindByName(string name)
        {
            try
            {
                var res = MF.AdvManager.FindByName(name);
                return this.JsonNet(new { success = true, data = res }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }


        public ActionResult DownloadList()
        {
            var res = MF.AdvManager.GetListBy(new AdvQD { Type = "模板" });
            return View(res);
        }



        [HttpPost]
        public ActionResult CreateDownload(AdvCUD im)
        {
            im.thumb = $"/Attachment/{Guid.NewGuid().ToString()}/{im.file.FileName}";
            var combinePath = Server.MapPath($"~{im.thumb }");

            Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

            im.file.SaveAs(combinePath);

            MF.AdvManager.Create(im);
            return this.JsonNet(new { succuess = true }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult CreateDownload()
        {
            
            var model = new AdvCUD();
            model.type = "模板";
            return View(model);
        }



        [HttpGet]
        public ActionResult UpdateDownload(int Id)
        {
           
            var model = MF.AdvManager.Find(Id);
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UpdateDownload(AdvCUD im)
        {
            if (im.file != null && im.file.ContentLength > 0)
            {
                im.thumb = $"/Attachment/{Guid.NewGuid().ToString()}/{im.file.FileName}";
                var combinePath = Server.MapPath($"~{im.thumb }");

                Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

                im.file.SaveAs(combinePath);
            }


            MF.AdvManager.Update(im);
            return this.JsonNet(new { succuess = true }, JsonRequestBehavior.AllowGet);
        }
    }


}